import { logLevelConst, logStyleConst } from '../consts/log-const';
import type logParamType from '../types/log-param-type';

/**
 * 日志工具
 */
export default class LogUtil {
  /**
   * 公共日志实现
   * @param params
   */
  static print(params: logParamType, level) {
    const defaultParams = {
      moduleName: '',
      msg: '',
      dev: true,
      prop: true,
    };

    const distParams = Object.assign(defaultParams, params);

    /*if (distParams.data && distParams.data instanceof Object) {
      distParams.data = JSON.stringify(distParams.data);
    } else {
      distParams.data = '';
    }*/

    if ((distParams.dev && import.meta.env.DEV) || (distParams.prop && import.meta.env.PROD)) {
      const log = `${new Date().toLocaleString()}  ${level}  ${distParams.moduleName} : ${
        distParams.msg
      } `;
      console.log(
        `%c Hippo Visual %c ${log}  `,
        'color:#fff;font-size:13px;background: linear-gradient(88deg, #18bbff 10%, #06bfcc 50%);border-radius: 4px;padding:1px;margin:4px',
        logStyleConst[level]
      );

      if (distParams.data) {
        console.log('%c%o', logStyleConst[level], distParams.data);
      }
    }
  }

  /**
   * 信息日志
   * @param params
   */
  static info(params: logParamType) {
    LogUtil.print(params, logLevelConst.INFO);
  }

  /**
   * 告警日志
   * @param params
   */
  static warn(params: logParamType) {
    LogUtil.print(params, logLevelConst.WARN);
  }

  /**
   * 错误日志
   * @param params
   */
  static error(params: logParamType) {
    LogUtil.print(params, logLevelConst.ERROR);
  }

  /**
   * 普通日志
   * @param params
   */
  static log(params: logParamType) {
    LogUtil.print(params, logLevelConst.LOG);
  }
}
